package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqFlocculationProduceMaintenanceMonthMapper;
import com.guofen.pojo.SjqFlocculationProduceMaintenanceMonth;
import com.guofen.service.sjq.SjqFlocculationProduceMaintenanceMonthService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SjqFlocculationProduceMaintenanceMonthServiceImpl implements SjqFlocculationProduceMaintenanceMonthService {

    @Autowired
    private SjqFlocculationProduceMaintenanceMonthMapper sjqFlocculationProduceMaintenanceMonthMapper;

    @Override
    public boolean add_recorder(String equipment_name, String is_normal, String operator, String maintenance_content, String remarks) {
        //获取当前日期
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String date = simpleDateFormat.format(now);
        //获取计划保养日期
        String maintenance_time_plan = "2022-01-01";
        //获取设备保养实施日期
        String maintenance_time_implement = DateAndTimeUtil.getDate();

        SjqFlocculationProduceMaintenanceMonth sjqFlocculationProduceMaintenanceMonth = new SjqFlocculationProduceMaintenanceMonth();
        sjqFlocculationProduceMaintenanceMonth.setDate(date);
        sjqFlocculationProduceMaintenanceMonth.setMaintenanceTimePlan(maintenance_time_plan);
        sjqFlocculationProduceMaintenanceMonth.setMaintenanceTimeImplement(maintenance_time_implement);
        sjqFlocculationProduceMaintenanceMonth.setEquipmentName(equipment_name);
        sjqFlocculationProduceMaintenanceMonth.setMaintenanceContent(maintenance_content);
        sjqFlocculationProduceMaintenanceMonth.setIsNormal(is_normal);
        sjqFlocculationProduceMaintenanceMonth.setRemarks(remarks);
        sjqFlocculationProduceMaintenanceMonth.setOperator(operator);
        int result = sjqFlocculationProduceMaintenanceMonthMapper.insert(sjqFlocculationProduceMaintenanceMonth);
        if (result == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean add_examiner(String id, String examiner, String remarks) {
        SjqFlocculationProduceMaintenanceMonth sjqFlocculationProduceMaintenanceMonth = new SjqFlocculationProduceMaintenanceMonth();
        sjqFlocculationProduceMaintenanceMonth.setId(Integer.parseInt(id));
        sjqFlocculationProduceMaintenanceMonth.setExaminer(examiner);
        sjqFlocculationProduceMaintenanceMonth.setRemarks(remarks);
        int result = sjqFlocculationProduceMaintenanceMonthMapper.updateByPrimaryKeySelective(sjqFlocculationProduceMaintenanceMonth);
        if (result == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public SjqFlocculationProduceMaintenanceMonth getlatest(String equipment_name, String maintenance_content) {
        return sjqFlocculationProduceMaintenanceMonthMapper.getlatest(equipment_name, maintenance_content);
    }

    @Override
    public List<SjqFlocculationProduceMaintenanceMonth> getlastByName(String equipmentName){
        return sjqFlocculationProduceMaintenanceMonthMapper.getlastByName(equipmentName);
    }

    @Override
    public List<Integer> is_lastweek_complete(String equipmentsName, String menstanceNum, String role) {
        List<Integer> res = new ArrayList<Integer>();
        String[] equipments = equipmentsName.split("_");
        String[] num = menstanceNum.split("_");
        boolean breakkey = false;

        for(int i=0;i<equipments.length ;i++){
            breakkey = false;
            List<SjqFlocculationProduceMaintenanceMonth> items = sjqFlocculationProduceMaintenanceMonthMapper.getlastByName(equipments[i]);
            if (items.size() == Integer.parseInt(num[i])){
                if(role.equals("examiner")){
                    for (SjqFlocculationProduceMaintenanceMonth item : items) {
                        if (item.getExaminer() == null || item.getExaminer().equals("")) {
                            res.add(1);
                            breakkey = true;
                            break;
                        }
                    }
                }
                if(breakkey)continue;
                res.add(2);
            }else{
                res.add(0);
            }
        }
        return res;
    }

    @Override
    public List<SjqFlocculationProduceMaintenanceMonth> getMonthRecords(String equipmentName) {
        return sjqFlocculationProduceMaintenanceMonthMapper.getMonthRecords(equipmentName);
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqFlocculationProduceMaintenanceMonth> list = sjqFlocculationProduceMaintenanceMonthMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqFlocculationProduceMaintenanceMonth> list = sjqFlocculationProduceMaintenanceMonthMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqFlocculationProduceMaintenanceMonthMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqFlocculationProduceMaintenanceMonth record) {
        return sjqFlocculationProduceMaintenanceMonthMapper.updateByPrimaryKeySelective(record);
    }
}
